package cn.gdcp.recyclerview;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rlvStu;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;

    private EditText edtKeyword;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudentArrayList();

        edtKeyword = findViewById(R.id.edt_keyword);
        btnSearch = findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = edtKeyword.getText().toString().trim();
                serch(studentArrayList, keyword);
                adapter.changeData(searchResultList);
            }
        });

        adapter = new StuAdapter(MainActivity.this, studentArrayList);
        rlvStu = findViewById(R.id.rlv_stu);
        rlvStu.setAdapter(adapter);

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);

        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    private void serch(ArrayList<Student> studentArrayList, String keyword) {
        searchResultList.clear();
        for (int i = 0; i<studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);

            if (s.getName().contains(keyword)){
                searchResultList.add(s);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.e("MainActivity",
                "requestCode = " + requestCode + "resultCode = " + requestCode);

        if (data == null){
            return;
        }

        String name = data.getStringExtra("NAME");
        int age = data.getIntExtra("AGE", 0);
        int imgId = data.getIntExtra("IMG", R.drawable.dog1);
        Student s = new Student(name, age, imgId);
        studentArrayList.add(s);
        adapter.notifyDataSetChanged();

    }

    private void initStudentArrayList() {
        Student s1 = new Student("张三", 20, R.drawable.dog1);
        Student s2 = new Student("李四", 18, R.drawable.dog2);
        Student s3 = new Student("王五", 19, R.drawable.dog3);
        Student s4 = new Student("赵六", 21, R.drawable.dog4);
        Student s5 = new Student("钱七", 22, R.drawable.dog5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }
}
